<?php
namespace Mentees\Controller;

class IndexController extends MainController {

    public function index(){
        if (!$this->baseInfo || $this->userBasic==0) {
            $this->redirect(U("Profile/index"));
        }

        $userID=$this->userID;
        $userInfo['mentors']=M("Apprentice")->where("toID=".$userID." and status=0")->count();
        $userInfo['project']=M("Train")->where("menteesID=".$userID."")->count();
        $userInfo['appointment']=M("Consult")->where("toID=".$userID." and status=0")->count();
        $userInfo['message']=M("Message")->where("toID=".$userID." and isread=0 and type=1 and toDel=0")->count();
        $this->assign("userInfo",$userInfo);

        //系统消息
        $sysmsg=M("Message")->where("toID=".$userID." and type=0 and isread=0")->order("id desc")->select();
        $this->assign("sysmsg",$sysmsg);

        //及时聊天信息
        $chat=M("Chat")->where("toID=".$userID."")->order("id desc")->limit("0,5")->select();
        $this->assign("chat",$chat);

        //今天需要做的事（导师预约与学习计划）
        $todo=array();
        $time1=strtotime(date('Y-m-d'));
        $time2=strtotime(date('Y-m-d',strtotime('+1 day')));
        $time="time>".($time1-1)." and time<".($time2+1);
        $i=0;
        $yuyue=M("Consult")->where("(toID=".$userID." or fromID=".$userID.") and status=1 and ".$time)->order("id desc")->select();
        foreach ($yuyue as $key => $value) {
            $todo[$i]['type']="appointment";
            if($value['toID'==$userID]){
               $todo[$i]['title']="【Appointment】Form:<b>".$value['fromName']."</b>"; 
            }else{
               $todo[$i]['title']="【Appointment】To:<b>".$value['fromName']."</b>"; 
            }
            $todo[$i]['time']=$value['createTime'];  
            $todo[$i]['link']=U('Appointment/edit',array('id'=>$value['id']));
            $i++;
        }
        unset($time);
        $pid=M("Plan")->where("menteesID=".$userID."")->getField("id",true);

        if ($pid) {
            $map['startTime']=array("lt",$time2+1);
            $map['endTime']=array("gt",$time2-1);
            $map['planID']=array('in',$pid);
            $map['status']=0;

            $plan=M("PlanDetails")->where($map)->order("id desc")->select();
            foreach ($plan as $key => $value) {
                $todo[$i]['type']="plan";
                $todo[$i]['title']="【Plan】".$value['title'].""; 
                $todo[$i]['link']=U('Plan/view',array('id'=>$value['planID'])); 
                $i++;
            }
        }
        
        $this->assign("todo",$todo);

    	$this->display();
    }

    //及时聊天
    public function chat(){
    	$obj=D("Chat");
    	if ($data = $obj->create()) {
            if ($list = $obj->add($data)) {
                $ret=array(
                	'state'=>'SUCCESS',
                	'message'=>$data
                	);
            } else {
            	$ret=array(
                	'state'=>'ERROR',
                	'message'=>L('_action_fail')
                	);
            }
        } else {
        	$ret=array(
                	'state'=>'ERROR',
                	'message'=>$obj->getError()
                	);
        }
        $this->ajaxReturn($ret);
    }

    //获取email的及时信息
    public function getEmailMsg(){
        $map['toID']=$this->userID;
        $map['isread']=0;
        $map['toDel']=0;
        $map['type']=1;
        $list=M("Message")->where($map)->order("id desc")->select();
        foreach ($list as $key => $value) {
            $list[$key]['fromFace']=getFace($value['fromFace']);
            $list[$key]['createTime']=mdate($value['createTime']);
        }
        $ret=array(
            "status"=>"SUCCESS",
            "total"=>count($list),
            "data"=>$list
            );
        $this->ajaxReturn($ret);
    }
     
    //获取系统通知
    public function getSysMsg(){
        $map['toID']=$this->userID;
        $map['isread']=0;
        $map['toDel']=0;
        $map['type']=0;
        $list=M("Message")->where($map)->order("id desc")->select();
        foreach ($list as $key => $value) {
            $list[$key]['createTime']=mdate($value['createTime']);
        }
        $ret=array(
            "status"=>"SUCCESS",
            "total"=>count($list),
            "data"=>$list
            );
        $this->ajaxReturn($ret);
    }

      //获取及时聊天信息
    public function getChatMsgLoop(){
        $endID=I("endID",0);
        $map['toID']=$this->userID;
        $map['isread']=0;
        $map['id']=array("GT",$endID);
        $list=M("Chat")->where($map)->order("id desc")->find();
        if($list){
            $list['fromFace']=getFace($value['fromFace']);
            $list['createTime']=mdate($value['createTime']);
            $ret=array(
                "status"=>"SUCCESS",
                "data"=>$list
                );
        }else{
            $ret=array(
                "status"=>"ERROR"
                );
        }
        $this->ajaxReturn($ret);
    }
}